<?php
    if ( isset( $_GET['action']) && $_GET['action'] == 'install' && isset( $_GET['name'] ) )
    {
        p::install( $_GET['name'] );
    }     

    if ( isset( $_GET['action']) && $_GET['action'] == 'remove' && isset( $_GET['name'] ) )
    {
        p::remove( $_GET['name'] );
    } 
?>
<h2>Плагины</h2>
<script type="text/javascript">
    $(document).ready(function(){

        $("div.main-text table tr:not(:first):odd").css("background", "#eee");    


        $(".install_plugin").click(
        function ()
        {
            var main = $("#main");
            var load = $("#load");

            load.fadeIn('slow');

            $.ajax({
                type: "POST", url: "admin.php?page=plugins&type=ajax&action=install&name="+this.id, data: "",

                complete: function(data)
                {
                    result = data.responseText;
                    main.html( result );
                    load.fadeOut('slow');
                }
            });
        }
        );    


        $(".tplug").click(
        function ()
        {
            var main = $("#main");
            var load = $("#load");

            $.ajax({
                type: "POST", url: "admin.php?page=plugins&type=ajax&name="+this.id, data: "",

                complete: function(data)
                {
                    result = data.responseText;
                    main.html( result );
                    load.fadeOut('slow');
                }
            });
        }
        );            

        $(".setting").click(
        function ()
        {
            var main = $("#main");
            var load = $("#load");

            load.fadeIn('slow');

            $.ajax({
                type: "POST", url: "admin.php?page=plugins.setting&type=ajax&name="+this.id, data: "",

                complete: function(data)
                {
                    result = data.responseText;
                    main.html( result );
                    load.fadeOut('slow');
                }
            });
        }
        );          

        $(".remove_plugin").click(
        function ()
        {
            var main = $("#main");
            var load = $("#load");

            load.fadeIn('slow');

            $.ajax({
                type: "POST", url: "admin.php?page=plugins&type=ajax&action=remove&name="+this.id, data: "",

                complete: function(data)
                {
                    result = data.responseText;
                    main.html( result );
                    load.fadeOut('slow');
                }
            });
        }
        );             



    });

</script>     
<div class="main-text" style="width:100%">
    <?php
        $plugin_array = p::get_list();

    ?>   
    <table width="100%" border="0">
    <tr><td valign="top">

        <table border="0" width="100%" cellspacing="2" cellpadding="2">
            <tr class="dataTableHeadingRow" align="left">
                <td class="dataTableHeadingContent" width="20px"><input type="checkbox"></td>
                <td class="dataTableHeadingContent" width="*" >Заголовок</td>
                <td class="dataTableHeadingContent" width="100px" >&nbsp;</td>
                <td class="dataTableHeadingContent" align="center" width="20px">Действие</td>   
            </tr>
            <?php
                //print_r($plugin_array);
                if ( count($plugin_array) > 0 )
                {
                    $color = '';
                    foreach ( $plugin_array as $name => $value )
                    {
                        if ( !isset($_GET['name']))
                        {
                            $_GET['name'] =  $name; 
                        }

                        $color = $color == '#f9f9ff' ? '#F7F7F7':'#f9f9ff';
                        echo '<tr class="tplug" id="'.$name.'">' . "\n"; 
                        echo '<td class="dataTableContent" width="20px"><input type="checkbox"></td>';
                        echo '<td class="dataTableContent" align="left" valign="middle">'.$value['title'].'</td>'. "\n"; 
                        echo '<td class="dataTableContent" align="center" valign="middle">'.$value['name'].'</td>'. "\n"; 

                        if ( (isset($value['installed']) and empty($value['installed'])) or !isset($value['installed']) )
                        {
                            $action  = '<a class="install_plugin" id="'.$value['name'].'">Установить</a>'; 
                        }
                        else
                        {
                            $action  = '<a class="remove_plugin" id="'.$value['name'].'">Удалить</a>';  
                        }

                        echo '<td class="dataTableContent" align="center" valign="middle">'.$action.'</td>'. "\n"; 
                        echo '</tr>';
                    }
                }
            ?>   
        </table>  
    </td><td valign="top" width="180px">


        <?php

            if ( isset($_GET['name']) and !empty($_GET['name']))
            {
                m::c('form');  
                $__name  = $_GET['name'];
                $setting = get_option('plugin_setting');

                echo '<table width="100%" cellspacing="2" cellpadding="2" border="0">
                
                <tr class="dataTableHeadingRow" align="left">
<td class="dataTableHeadingContent" width="20px" style="color:green; text-align:center">'.$__name.'</td></tr><tr><td>
                <div style="padding: 5px;">';
                
                if ( isset($setting[$__name]) )
                {
                    $setting = $setting[$__name];  

                    if ( is_array($setting) and count($setting) > 0)
                    {
                        $f = new form(); 
                        $f->class='setting_class';
                        $color = '';
                        foreach ($setting as $num => $value)
                        {
                            if ($value['type'] == 'select')
                            {
                                $opt = array();

                                foreach ($value['option'] as $va)
                                {
                                    $sel = '';
                                    if ($va == $value['value'] ) $sel = 'selected' ;
                                    $opt [] = array(
                                    0=> $va,
                                    1=> $va,
                                    2=> $sel
                                    );  
                                }

                                $color = $color == '#eee' ? '#fff':'#eee';   
                                $f->clform[] = $value['title'].'<br>';
                                $f->add($value['type'], '', $num,  $value['value'], '', $opt);
                            }

                            if ($value['type'] == 'text')
                            {
                                $color = $color == '#eee' ? '#fff':'#eee';   
                                $f->maxlength = 100;
                                $f->clform[] = $value['title'].'<br>';
                                $f->add('input', '', $num,  $value['value'], '', $opt);
                            }   
                            // echo $value['value'].' '.$value['title'].'<br>';
                        }

                        echo '<table class="table_setting" width="100%" border="0" cellpadding="3">';     
                        echo $f->get_form();  
                        echo '</table>

                        ';     
                    }  
                }   

                if ( isset( p::$name_action [$__name] ['page'] ) and count(p::$name_action [$__name] ['page']) > 0 )
                {
                    echo 'Страницы<br>';
                    echo '<div style="padding-left: 10px;">'; 
                    foreach ( p::$name_action [$__name] ['page']  as $id => $name)
                    {
                        echo '<a href="index.php?p='.$name.'">'.$name.'</a><br>';
                    }
                    echo '</div>';
                }  


            }
            
            echo '                        </td></tr>
                        </table>';
        ?>
    
</div>

</td> </tr>   
</table>




<table width="100%">
    <tr>
        <td>
            <?php       
                echo '<i>Всего: '.p::total().'</i>';
            ?>
        </td>
    </tr>   
    </table>
</div>